/**
 * 
 */
package com.user_hosted_storage.server.utils;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
import org.jivesoftware.smack.packet.Message;

import com.google.gson.Gson;
import com.user_hosted_storage.dto.BaseRequest;
import com.user_hosted_storage.server.bl.ServerProperties;


/**
 * @author Administrator
 *
 */
public class XMPPUtils {

	private static Logger log = Logger.getLogger(XMPPUtils.class.getName());

	private static final String JABBER_ATTRIBUTE_NAME  			  = "Name";
	private static final int JABBER_ALREADY_EXISTS_ERROR_CODE  	  = 409;

	public static void sendMessage(String recepient, BaseRequest content){



		XMPPConnection adminConnection = null;
		try {
			ServerProperties serverProperties = PropertiesParserSingleton.get(ServerProperties.class);
			ConnectionConfiguration connConfig = new ConnectionConfiguration(serverProperties.XMPP_HOST,serverProperties.XMPP_PORT);

			connConfig.setSASLAuthenticationEnabled(false);

			adminConnection = new XMPPConnection(connConfig);
			adminConnection.connect();
			adminConnection.login(serverProperties.XMPP_ADMIN_USER,serverProperties.XMPP_ADMIN_PASSWORD);
			String jSon = new Gson().toJson(content);
			System.out.println("Sending admin message to: "+recepient + " Content: "+jSon);
			Chat chat = adminConnection.getChatManager().createChat(recepient+"@rushphone.ru", new MessageListener() {

				@Override
				public void processMessage(Chat arg0, Message arg1) {
				}
			});
			Message message = new Message();
			message.setTo(recepient+"@rushphone.ru");
			message.setBody(jSon);
			chat.sendMessage(message);
		} catch(XMPPException e){
			log.log(Level.SEVERE, "", e);
		} catch (PropertyException e) {
			log.log(Level.SEVERE, "", e);
		} finally{
			if(adminConnection != null){
				adminConnection.disconnect();
			}
		}
	}

	public static void jabberRegister (String userName, String password) throws XMPPException {
		System.out.println("Registering: "+userName + ":"+password);
		XMPPConnection adminConnection = null;
		try {
			SmackConfiguration.setLocalSocks5ProxyEnabled(false);
			ServerProperties serverProperties = PropertiesParserSingleton.get(ServerProperties.class);
			System.out.println("XMPP_HOST: "+serverProperties.XMPP_HOST);
			ConnectionConfiguration connConfig = new ConnectionConfiguration(serverProperties.XMPP_HOST,serverProperties.XMPP_PORT);

			connConfig.setSASLAuthenticationEnabled(false);
			connConfig.setSecurityMode(SecurityMode.disabled);
			
			adminConnection = new XMPPConnection(connConfig);
			adminConnection.connect();
			adminConnection.login(serverProperties.XMPP_ADMIN_USER,serverProperties.XMPP_ADMIN_PASSWORD);
			Map<String,String> attributers = new HashMap<String, String>();
			attributers.put(JABBER_ATTRIBUTE_NAME, userName);
			adminConnection.getAccountManager().createAccount(userName, password);
		} catch(XMPPException e){
			e.printStackTrace();
			if(e.getXMPPError().getCode() == JABBER_ALREADY_EXISTS_ERROR_CODE) {
				// nothing to do - can happen only as result of server crash
			} else {
				throw e;
			}
		} catch (PropertyException e) {
			log.log(Level.SEVERE, "", e);
		} finally{
			if(adminConnection != null){
				adminConnection.disconnect();
			}
		}
	}
	
	public static void changePassword(String login, String password, String newPassword){
		XMPPConnection adminConnection = null;
		try {


			ServerProperties serverProperties = PropertiesParserSingleton.get(ServerProperties.class);
			ConnectionConfiguration connConfig = new ConnectionConfiguration(serverProperties.XMPP_HOST, serverProperties.XMPP_PORT);

			connConfig.setSASLAuthenticationEnabled(false);

			adminConnection = new XMPPConnection(connConfig);
			adminConnection.connect();
			adminConnection.login(login, password);
			adminConnection.getAccountManager().changePassword(newPassword);
			
		}catch (Exception e) {
			log.log(Level.SEVERE, "", e);
		} finally{
			if(adminConnection != null){
				adminConnection.disconnect();
			}
		}

	}

	public static void removeClient(String login, String password) {
		XMPPConnection adminConnection = null;
		try {

			ServerProperties serverProperties = PropertiesParserSingleton.get(ServerProperties.class);
			ConnectionConfiguration connConfig = new ConnectionConfiguration(serverProperties.XMPP_HOST,serverProperties.XMPP_PORT);

			connConfig.setSASLAuthenticationEnabled(false);

			adminConnection = new XMPPConnection(connConfig);
			adminConnection.connect();
			adminConnection.login(login,password);
			adminConnection.getAccountManager().deleteAccount();
			
		}catch (Exception e) {
			log.log(Level.SEVERE, "", e);
		} finally{
			if(adminConnection != null){
				adminConnection.disconnect();
			}
		}
	}
	
	public static void main(String[] args) {
		XMPPConnection adminConnection = null;
		try {


//			ServerProperties serverProperties = PropertiesParserSingleton.get(ServerProperties.class);
			ConnectionConfiguration connConfig = new ConnectionConfiguration("flirtnavigator.com",5222);

			connConfig.setSASLAuthenticationEnabled(false);
			connConfig.setSecurityMode(SecurityMode.required);
			connConfig.setSelfSignedCertificateEnabled(true);
			adminConnection = new XMPPConnection(connConfig);
			
			adminConnection.connect();
			adminConnection.login("admin","quickies");
			Map<String,String> attributers = new HashMap<String, String>();
			String userName = "manii123";
			attributers.put(JABBER_ATTRIBUTE_NAME, userName);
			adminConnection.getAccountManager().createAccount((new StringBuilder(String.valueOf(userName))).toString(), "asdert");
		} catch(XMPPException e){
			e.printStackTrace();
		} finally{
			if(adminConnection != null){
				adminConnection.disconnect();
			}
		}
	}
	
	
}
